package cheme.ProcessEditor.Policies;

import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.editpolicies.GraphicalNodeEditPolicy;
import org.eclipse.swt.graphics.Color;

/*
 * A first stab at hooking up a feedback mechanism. For now this simply responds 
 * to one kind of request and does a silly color change when the request is allowed.
 * I'd like to add a mroe general mechanism by which an annotation can be added to the
 * figure by placing it in the feedback layer and oreinting it to the annotee via a locator.
 * 
 * I've done this before in Chunks and it should be relatively easy to do.
 * 
 * One new twist: to generalize it I could make a FeedbackRule type that can be added
 * to this FeedbackPolicy.
 */
public abstract class HighlightFeedbackPolicy extends GraphicalNodeEditPolicy
{
	private boolean allowHighlightFeedback = false;

	private Color revertColor;

	public boolean allowHighlightFeedback()
	{
		return allowHighlightFeedback;
	}

	public void setAllowHighlightFeedback(boolean allowHighlightFeedback)
	{
		this.allowHighlightFeedback = allowHighlightFeedback;
	}

	public void eraseTargetFeedback(Request request)
	{
		if(revertColor != null)
		{
			setAllowHighlightFeedback(false);
			setContainerBackground(revertColor);
			revertColor = null;
		}
	}

	private IFigure getContainerFigure()
	{
		return ((GraphicalEditPart)getHost()).getContentPane();
	}

	private Color getContainerBackground()
	{
		return getContainerFigure().getBackgroundColor();
	}

	private void setContainerBackground(Color c)
	{
		getContainerFigure().setBackgroundColor(c);
	}

	public void showTargetFeedback(Request request)
	{
		if(allowHighlightFeedback())
		{
			showHighlight();
		}
	}

	protected void showHighlight()
	{
		if(revertColor == null)
		{
			revertColor = getContainerBackground();
			setContainerBackground(new Color(null, 200, 255, 200));
		}
	}
}
